package com.atg.ai_agent.tools;


/*
author: atg
time: 2025/10/6 11:11
*/

import cn.hutool.core.io.FileUtil;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

import static com.atg.ai_agent.constant.FileConstant.FILE_PATH;

/**
 * 文件操作工具类
 */
public class FileOperationTool {

    private final static String FILE_SEPARATOR = FILE_PATH + "/file";

   /**
     * 读取文件内容
     * @param fileName 文件名
     * @return 文件内容
     */
    @Tool(description = "Read file content")
    public String readFile(@ToolParam(description = "The file name that needs to be read") String fileName) {
        String filePath = FILE_SEPARATOR + "/" + fileName;
        try {
            return FileUtil.readUtf8String(filePath);
        } catch (Exception e) {
            return "Error reading file: " + e.getMessage();
        }

    }

    /**
     * 写入文件内容
     * @param fileName 文件名
     * @param content 文件内容
     * @return 文件写入结果
     */
    @Tool(description = "Write file content")
    public String writeFile(@ToolParam(description = "The file name that needs to be written") String fileName,
                            @ToolParam(description = "The content that needs to be written") String content) {

        String filePath = FILE_SEPARATOR + "/" + fileName;
        try {
            FileUtil.mkdir(FILE_SEPARATOR);
//            FileUtil.writeString(content, filePath, "UTF-8");
            FileUtil.writeUtf8String(content, filePath);
            return "File written successfully";
        } catch (Exception e) {
            return "Error writing file: " + e.getMessage();
        }
    }

}
